package com.wanmait.movies.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.wanmait.movies.util.DBHelper;
import com.wanmait.movies.vo.Collect;
import com.wanmait.movies.vo.Movie;
import com.wanmait.movies.vo.MovieCountry;
import com.wanmait.movies.vo.MovieLanguage;
import com.wanmait.movies.vo.MovieType;
import com.wanmait.movies.vo.Pager;


public class CollectDao {
	private CollectDao() {}
	private static CollectDao collectDao = new CollectDao();
	public static CollectDao getObj() {
		return collectDao;
	}

	public ArrayList<Collect> getcollectdao(int userid,int thispage){//��ȡ�ҵ��ղص�Ӱ
	ArrayList<Collect>allcollect=new ArrayList<Collect>();
	 Connection con=DBHelper.getDBHelper().getconn();
	 PreparedStatement ps=null;
	 ResultSet rs=null;
	 StringBuilder sb=new StringBuilder();
		sb.append(" select * from ");
		sb.append(" collect,`user`,movie,movie_language,movie_type,movie_country ");
		sb.append(" WHERE ");
		sb.append(" user_id=user.id ");
		sb.append(" and ");
		sb.append(" user_id=? ");
		sb.append(" and ");
		sb.append(" movie_type_id=movie_type.id  ");
		sb.append(" and ");
		sb.append(" movie_country_id=movie_country.id  ");
		sb.append(" and ");
		sb.append(" movie_id=movie.id ");
		sb.append(" and ");
		sb.append(" movie_language_id=movie_language.id  ");
		sb.append(" order by collect.id desc ");
		sb.append(" limit ?,5 ");
	 String sql=sb.toString();
	 try {
		ps=con.prepareStatement(sql);
		ps.setInt(1, userid);
		ps.setInt(2, (thispage-1)*5);
	    rs=ps.executeQuery();
	    while(rs.next()) {
	    	Collect collect=new Collect();
	    	collect.setId(rs.getInt("collect.id"));
	    	Movie movie=new Movie();
	    	movie.setId(rs.getInt("movie.id"));//��Ӱid
	    	movie.setMovieImage(rs.getString("movie_image"));//����ͼ
	    	movie.setMovieName(rs.getString("movie_name"));//��Ӱ����
	    	MovieLanguage language=new MovieLanguage();
	    	language.setId(rs.getInt("movie_language.id"));//��Ӱ����
	    	language.setLanguageType(rs.getString("language_type"));//����
	    	movie.setMovieLanguageId(language);
	    	movie.setMovieShowtime(rs.getTimestamp("movie_showtime"));//��ӳʱ��
	    	MovieCountry country=new MovieCountry();
	    	country.setId(rs.getInt("movie_country.id"));
	    	country.setCountry(rs.getString("country"));//����
	    	movie.setMovieCountryId(country);
	    	MovieType type=new MovieType();
	    	type.setId(rs.getInt("movie_type.id"));//��Ӱ����
	    	type.setMovieType(rs.getString("movie_type"));
	    	movie.setMovieTypeId(type);
	    	movie.setMovieShow(rs.getBoolean("movie_show"));//�Ƿ���ӳ
	    	movie.setMovieTime(rs.getInt("movie_time"));//ʱ��
	    	movie.setMovieIntroduction(rs.getString("movie_introduction"));//��Ӱ���
	    	collect.setMovieId(movie);
	    	allcollect.add(collect);
	    }
	    return allcollect;
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	 finally {
		 DBHelper.getDBHelper().close(rs, ps, con);
	 }
	 return null;
	}
	
	public int getPageMax(int userId) {//���ݴ���������id��Ϊ�����鿴���ղ��˼�����Ӱ
	      Connection con=DBHelper.getDBHelper().getconn();
	 		PreparedStatement ps=null;
	 		ResultSet rs=null;
	 		int max=0;
	 		String sql="select count(id) as m from collect where user_id=?";
	 		try {
	 			ps=con.prepareStatement(sql);
	 			ps.setInt(1, userId);
	 			rs=ps.executeQuery();
	 			rs.next();
	 			max=rs.getInt("m");
	 		} catch (SQLException e) {
	 			// TODO Auto-generated catch block
	 			e.printStackTrace();
	 		}
	 		return max;
	     }
	public void CollectDelete(String collectid) {
		Connection con=	DBHelper.getDBHelper().getconn();
		PreparedStatement ps=null;
		String sql="delete from collect where id in("+collectid+")";
		try {
			ps=con.prepareStatement(sql);
			ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		DBHelper.getDBHelper().close(null, ps, con);
	}
		
	
	
	
	
	
	

	public ArrayList<Collect> getcollectdao(Pager pager, int id){//��ȡ�ҵ��ղص�Ӱ
		ArrayList<Collect>allcollect=new ArrayList<Collect>();
		 Connection con=DBHelper.getDBHelper().getconn();
		 PreparedStatement ps=null;
		 ResultSet rs=null;
		 /*StringBuilder sb=new StringBuilder();
			sb.append(" select * from ");
			sb.append(" collect,`user`,movie,movie_language,movie_type,movie_country ");
			sb.append(" WHERE ");
			sb.append(" user_id=user.id ");
			sb.append(" and ");
			sb.append(" user_id=? ");
			sb.append(" and ");
			sb.append(" movie_type_id=movie_type.id  ");
			sb.append(" and ");
			sb.append(" movie_country_id=movie_country.id  ");
			sb.append(" and ");
			sb.append(" movie_id=movie.id ");
			sb.append(" and ");
			sb.append(" movie_language_id=movie_language.id  ");
			sb.append(" order by collect.id desc ");
			sb.append(" limit ?,5 ");
		 String sql=sb.toString();*/
		 String sql = "select * from  collect,movie WHERE user_id = ? AND movie.id=collect.movie_id order by collect.id desc LIMIT ?,5";
		 try {
			ps=con.prepareStatement(sql);
			ps.setInt(1, id);
			System.out.println((pager.getCurrentPager()-1)*5);
			ps.setInt(2, (pager.getCurrentPager()-1)*5 );
		    rs=ps.executeQuery();
		    while(rs.next()) {
		    	Collect collect = new Collect();
		    	collect.setId(rs.getInt("collect.id"));
		    	Movie movie=new Movie();
		    	movie.setId(rs.getInt("movie.id"));//��Ӱid
		    	movie.setMovieImage(rs.getString("movie_image"));//����ͼ
		    	movie.setMovieName(rs.getString("movie_name"));//��Ӱ����
		    	movie.setMovieShowtime(rs.getTimestamp("movie_showtime"));//��ӳʱ��
		    	collect.setMovieId(movie);
		    	allcollect.add(collect);
		    	/*
		    	Collect collect=new Collect();
		    	collect.setId(rs.getInt("collect.id"));
		    	Movie movie=new Movie();
		    	movie.setId(rs.getInt("movie.id"));//��Ӱid
		    	movie.setMovieImage(rs.getString("movie_image"));//����ͼ
		    	movie.setMovieName(rs.getString("movie_name"));//��Ӱ����
		    	MovieLanguage language=new MovieLanguage();
		    	language.setId(rs.getInt("movie_language.id"));//��Ӱ����
		    	language.setLanguageType(rs.getString("language_type"));//����
		    	movie.setMovieLanguageId(language);
		    	movie.setMovieShowtime(rs.getTimestamp("movie_showtime"));//��ӳʱ��
		    	MovieCountry country=new MovieCountry();
		    	country.setId(rs.getInt("movie_country.id"));
		    	country.setCountry(rs.getString("country"));//����
		    	movie.setMovieCountryId(country);
		    	MovieType type=new MovieType();
		    	type.setId(rs.getInt("movie_type.id"));//��Ӱ����
		    	type.setMovieType(rs.getString("movie_type"));
		    	movie.setMovieTypeId(type);
		    	movie.setMovieShow(rs.getBoolean("movie_show"));//�Ƿ���ӳ
		    	movie.setMovieTime(rs.getInt("movie_time"));//ʱ��
		    	movie.setMovieIntroduction(rs.getString("movie_introduction"));//��Ӱ���
		    	collect.setMovieId(movie);
		    	allcollect.add(collect);
		    	*/
		    }
		     
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 finally {
			 DBHelper.getDBHelper().close(rs, ps, con);
		 }
		 return allcollect;
		}
		
	public void shoucang(int userId,int movieId) {
		Connection co = DBHelper.getDBHelper().getconn();
		String sql="insert into collect(user_id,movie_id) values(?,?)";
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps=co.prepareStatement(sql);
			ps.setInt(1, userId);
			ps.setInt(2, movieId);
			ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			DBHelper.getDBHelper().close(rs, ps, co);
		}
	}
	
	
	//���û����ղ�ɾ��
		public void quxiaoshoucang(int userId,int movieId) {
			Connection co = DBHelper.getDBHelper().getconn();
			String sql="DELETE FROM  collect where user_id=? and movie_id=?";
			PreparedStatement ps = null;
			ResultSet rs = null;
			try {
				ps=co.prepareStatement(sql);
				ps.setInt(1, userId);
				ps.setInt(2, movieId);
				ps.executeUpdate();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			finally {
				DBHelper.getDBHelper().close(rs, ps, co);
			}

		}
}























